$(function(){
	var service = new EbeimService();
    var jform = $("#j-form"),
        jsubmit = $("#j-submit"),
        jphone = $("#j-phone");;
    
    var jbindDevice = $("#j-bindDevice");
	var jbindDeviceName = $("#j-bindDeviceName");

	jphone.val(plugin.GetQueryString("phone"));
    //占位符初始化
    plugin.placeHolder({
        con: $(".j-placeHolder")
    });

    //表单验证初始化
    jform.find(".j-formcheck").bind("blur", function() {
        var t = $(this);
        var name = t.attr("data-name");
        formCheck(jform,name);
    }).bind('focus',function() {
        var t = $(this);
        var error = t.siblings('.error');
        error.hide();
    });
    
    service.getComputerMessage({},function(json){
		if(json.code == 200) {
			jbindDevice.html(json.bindDevice);
			jbindDevice.siblings("[name='bindDevice']").val(json.bindDevice);
			jbindDeviceName.val(json.bindDeviceName);
		}else{
			plugin.msgtip({type: "i",msg: json.desc});
        }
	});

    //表单提交
    jform.bind("submit", function(){
        if (formCheck(jform)) {
            jsubmit.attr("disabled", "disabled");
            
            var labType = plugin.GetQueryString("labType");
            var data = jform.serialize();
            
            data = data + "&username="+ plugin.GetQueryString("username");
            
            if(labType == "1" || labType == "3"){
            	service.keyNoBindCheck(data, function(json){
                    if(json.code == 200) {
                    	plugin.msgtip({type: "o",msg: json.desc});
                    	setTimeout(function(){
                    		window.location.href = "../index.html";
                        }, 1500);
                    }else{
                    	jsubmit.removeAttr("disabled");
                    	plugin.msgtip({type: "i",msg: json.desc});
                    }
                });
            }else{
            	service.authorizationCodeNoBindCheck(data, function(json){
                    if(json.code == 200) {
                    	plugin.msgtip({type: "o",msg: json.desc});
                    	setTimeout(function(){
                    		window.location.href = "../index.html";
                        }, 1500);
                    }else{
                    	jsubmit.removeAttr("disabled");
                    	plugin.msgtip({type: "i",msg: json.desc});
                    }
                });
            }
        }
        return false;
    });

    //记录定时器
    var timer = "";
    var countDown = function(dom,time){
        dom.val(time+"秒");
        timer = setInterval(function(){
            if(time>0){
                time--;
                dom.val(time+"秒");
            }else{
                clearInterval(timer);
                dom.removeAttr("disabled").removeClass("disableBtn").addClass("codeBtn").val("获取短信验证码");
            }
        },1000);
    }
    //绑定短信发送事件
    jform.delegate(".j-getCode","click",function(){
            var data = {
                token:"tokennawang",
                username: plugin.GetQueryString("username")

            }
            var t = $(this);
            t.attr("disabled", "disabled").addClass("disableBtn");
            countDown(t,120);
            service.getMessageCode(data,function(json){
                if(json.code == 200) {
                	plugin.msgtip({type: "o",msg: json.desc});
                }else{
                	plugin.msgtip({type: "i",msg: json.desc});
                }
            })
        });

})

// 表单验证
var formCheck = function(form,name) {

    // 显示错误信息
    var showError = function(obj, msg) {
    	var error = obj.siblings('.error');
        msg = msg + "<i></i>"
        error.html(msg);
        error.show();
    };

    // 隐藏错误信息
    var hideError = function(obj) {
        var error = obj.siblings('.error');
        error.hide();
    };

    // 数据验证对象
    var dataCheck = {
        code: {
            obj: form.find("[data-name='code']"),
            fn: function() {
                var val = this.obj.val();
                if (plugin.checkFn.isNull(val)) {
                    showError(this.obj,"请输入短信验证码");
                    return false;
                } else {
                    hideError(this.obj);
                    return true;
                }
            }
        },
        bindDeviceName: {
            obj: form.find("[data-name='bindDeviceName']"),
            fn: function() {
                var val = this.obj.val();
                if (plugin.checkFn.isNull(val)) {
                    showError(this.obj,"请输入设备名称");
                    return false;
                } else {
                    hideError(this.obj);
                    return true;
                }
            }
        }
    };

    if (name && dataCheck[name]) {
        return dataCheck[name].fn();
    } else {
        var errLen = 0;
        for (var i in dataCheck) {
            if (!dataCheck[i].fn()) {
                errLen++;
            }
        }
        if (errLen > 0) {
            return false;
        }
        return true;
    }
}

